package com.xcity.game.map;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.xcity.db.entity.Mine;
import com.xcity.db.entity.Player;
import com.xcity.pomelo.push.msg.DotTipPush;

import naga.x.game.time.TimeUpdater;

/**
 * 战报
 * @author shipeng.zhu
 */
public class MineReportManager {
	protected Player owner;
	protected List<MineReport> reports;
	
	public static int OUT_TIME = 3 * 24 * 3600 * 1000;
	public static int MAX_LIST_SIZE = 40;
	
	/**
	 * 战报类型 掠夺目标失败
	 */
	public static int REPORT_TYPE_ATK_LOSE = 0;
	/**
	 * 战报类型 掠夺目标成功
	 */
	public static int REPORT_TYPE_ATK_WIN = 1;
	/**
	 * 战报类型 被目标掠夺失败(防御失败)
	 */
	public static int REPORT_TYPE_DEF_LOSE = 2;
	/**
	 * 战报类型 被目标掠夺成功(防御成功)
	 */
	public static int REPORT_TYPE_DEF_WIN = 3;
	
	/**
	 * 战报类型 收矿信息
	 */
	public static int REPORT_TYPE_MINE_UNTAKE = 4;
	
	public MineReportManager(Player owner, List<MineReport> reportList){
		this.owner = owner;
		this.reports = reportList != null? reportList : new ArrayList<MineReport>();
	}
	
	public void addNewMineReport(Player target, Mine mine, int reportType, long time){
		filterList();
		if(reports.size() >= MAX_LIST_SIZE){
			reports.remove(0);
		}
		MineReport newReport = new MineReport(mine, target, time, reportType);
		reports.add(newReport);
		
		if(owner.isOnline()){
			owner.send(DotTipPush.NEW_MINE_REPORT);
		}
	}
	
	public void filterList(){
		long deadline = TimeUpdater.getInstance().now() - OUT_TIME;
		for(int i = reports.size() - 1; i > -1; i--){
			MineReport report = reports.get(i);
			if(report.time < deadline){
				reports.remove(i);
			}
		}
	}
	public List<MineReport> getReports() {
		return reports;
	}
	
	@Override
	public String toString(){
		return JSON.toJSONString(reports);
	}

	public void setOwner(Player owner) {
		this.owner = owner;
	}
}
